﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DiaryPlanner
{
    public partial class PlannerEdit : Form
    {
        public const int DAY = 0;
        public const int WEEK = 1;
        public const int MONTH = 2;

        private const int morning = 0;
        private const int after = 1;

        private Planner planner;
        private PlannerDay plannerDay;
        private PlannerWeek plannerWeek;

        public PlannerEdit()
        {
            InitializeComponent();
        }

        private void initPlannerEdit()
        {
            if (plannerDay != null)
            {
                timeTextBox1.Text = planner.monthCalendar1.SelectionStart.Date.ToString().Substring(0, 10);
                timeTextBox2.Text = plannerDay.dayTime(morning);
                timeTextBox3.Text = planner.monthCalendar1.SelectionStart.Date.ToString().Substring(0, 10);
                timeTextBox4.Text = plannerDay.dayTime(after);
                
            }
            else if (plannerWeek != null)
            {
            }
            else
            {
                timeTextBox1.Text = planner.monthCalendar1.SelectionStart.Date.ToString().Substring(0, 10);
                timeTextBox3.Text = planner.monthCalendar1.SelectionStart.Date.ToString().Substring(0, 10);
            }
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            CalendarConnection.Instance().addCalendar(titleTextBox.Text, richTextBox1.Text,locationTextBox.Text, 
                                                      parseDateTime(timeTextBox1.Text+"."+timeTextBox2.Text), 
                                                      parseDateTime(timeTextBox3.Text+"."+timeTextBox4.Text));
            
            this.Close();
        }

        private void delButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private DateTime parseDateTime(string time)
        {
            int index = 0;

            int year, month, day, hour, minute;
            
            year = int.Parse(time.Substring(0, index = time.IndexOf("-", index)));
            month = int.Parse(time.Substring(++index,2));
            index = index + 2;
            day = int.Parse(time.Substring(++index, 2));
            index = index + 2;
            hour = int.Parse(time.Substring(++index, 2));
            index = index + 2;
            minute = int.Parse(time.Substring(++index, 2));

            DateTime dateTime = new DateTime(year, month, day, hour, minute, 0);
            return dateTime;
        }

        public void initPlannerEdit(Planner planner)
        {
            this.planner = planner;
            initPlannerEdit();
        }

        public void initPlannerEdit(Planner planner, PlannerDay plannerDay)
        {
            this.plannerDay = plannerDay;
            initPlannerEdit(planner);
        }

        public void initPlannerEdit(Planner planner, PlannerWeek plannerWeek)
        {
            this.plannerWeek = plannerWeek;
            initPlannerEdit(planner);
        }
    }
}
